package jmc.util;

public class Roman {

	   final static RomanValue[] ROMAN_VALUE_TABLE = {
	        new RomanValue(1000, "M"),
	        new RomanValue( 900, "CM"),
	        new RomanValue( 500, "D"),
	        new RomanValue( 400, "CD"),
	        new RomanValue( 100, "C"),
	        new RomanValue(  90, "XC"),
	        new RomanValue(  50, "L"),
	        new RomanValue(  40, "XL"),
	        new RomanValue(  10, "X"),
	        new RomanValue(   9, "IX"),
	        new RomanValue(   5, "V"),
	        new RomanValue(   4, "IV"),
	        new RomanValue(   1, "I")
	    };
	    
	    
	    public static String convertToRoman(int n) {
	        if (n >= 4000  || n < 1) {
	            throw new NumberFormatException("Numbers must be in range 1-3999");
	        }
	        StringBuffer result = new StringBuffer(10);
	        
	        
	        for (RomanValue equiv : ROMAN_VALUE_TABLE) {
	            
	            while (n >= equiv.intVal) {
	                n -= equiv.intVal;            
	                result.append(equiv.romVal); 
	            }
	        }
	        return result.toString();
	    }
	    
	    
	    private static class RomanValue {
	        int    intVal;     
	        String romVal;     
	        
	        RomanValue(int dec, String rom) {
	            this.intVal = dec;
	            this.romVal = rom;
	        }
	    }
}
